
let promise = (middlewareAPI) => {
  return next => {
    return action => {
      console.log('promise')
      next(action)
    }
  }
}

let thunk = middlewareAPI => { // middlewareAPI 层
  return next => { // next 层
    return action => { // action 层
      console.log('thunk')
      next(action)
    }
  }
}

let logger = middlewareAPI => { // middlewareAPI 层
  return next => { // next 层
    return action => { // action 层
      console.log('logger')
      next(action)
    }
  }
}

// 把所有的 redux 中间件放到 chain 中
const chain = [promise, thunk, logger].map(fn => fn({}))


function compose(middleawres) {
  return middleawres.reduce((a,b) => {
    debugger
    return (next) => {
      return a(b(next))
    }
  })
}

const dispatch = (action) => {
  console.log('原始的 dispatch 方法', action)
}

const composed = compose(chain)

const newDispatch = composed(dispatch)
newDispatch({ type: 'ADD' })
